package com.zzt.appframe8.widget;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;

import com.google.android.material.internal.CheckableImageButton;
import com.zzt.appframe8.R;

/**
 * @author: zeting
 * @date: 2022/9/1
 * 密码切换图标工具
 * {@link com.google.android.material.textfield.PasswordToggleEndIconDelegate )
 */
@SuppressLint("RestrictedApi")
class PasswordToggleEndIconDelegate extends EndIconDelegate {

    private CheckableImageButton imageBtn; // 当前可操作按钮
    private boolean enableImgBtn;// 当前按钮是否可操作状态

    PasswordToggleEndIconDelegate(@NonNull MTextInputLayout textInputLayout) {
        super(textInputLayout);
    }

    @Override
    void initialize() {
        if (imageBtn != null) {
            Drawable drawable = imageBtn.getDrawable();
            if (drawable == null) {
                imageBtn.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.design_password_eye));
                refreshIconDrawableState(imageBtn, AppCompatResources.getColorStateList(context, R.color.m_text_btn_text_color_selector));
            }
            imageBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText editText = textInputLayout.getEditText();
                    if (editText == null) {
                        return;
                    }
                    // Store the current cursor position
                    final int selection = editText.getSelectionEnd();
                    if (hasPasswordTransformation()) {
                        editText.setTransformationMethod(null);
                    } else {
                        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                    // And restore the cursor position
                    if (selection >= 0) {
                        editText.setSelection(selection);
                    }
//                    refreshIconDrawableState(imageBtn, AppCompatResources.getColorStateList(context, R.color.m_text_btn_text_color_selector));
                    imageBtn.setChecked(hasPasswordTransformation());
                }
            });
            EditText editText = textInputLayout.getEditText();
            if (isInputTypePassword(editText) && enableImgBtn) {
                editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                imageBtn.setVisibility(View.VISIBLE);
            } else {
                imageBtn.setVisibility(View.GONE);
            }
            imageBtn.setChecked(hasPasswordTransformation());
        }
    }

    private boolean hasPasswordTransformation() {
        EditText editText = textInputLayout.getEditText();
        return editText != null && editText.getTransformationMethod() instanceof PasswordTransformationMethod;
    }

    private static boolean isInputTypePassword(EditText editText) {
        return editText != null
                && (editText.getInputType() == (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD)
                || editText.getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)
                || editText.getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD)
                || editText.getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
    }

    // 0x00000001    00000001
    // 0x00000080    10000000

    public void setImageBtnEnable(Boolean enableImageBtn, CheckableImageButton imageButton) {
        this.enableImgBtn = enableImageBtn;
        if (enableImageBtn) {
            this.imageBtn = imageButton;
        }
        imageButton.setVisibility(enableImageBtn ? View.VISIBLE : View.GONE);
    }

}
